package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.utils.AliyunOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

//外部配置属性批量导入注解方式，属性名一定要同名，才会自动注入映射
//@ConfigurationProperties(prefix = "aliyun.oss")
//这个不知道为什么没成功！！！！！！！！！ERROR
@Slf4j
@RestController
public class UploadController {
    @Value("${aliyun.oss.endpoint}")
//    外部数据单个导入
    private String endpoint;
    @Value("${aliyun.oss.bucketName}")
//    外部数据单个导入
    private String bucketName;


    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        try {
            String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            return Result.success(AliyunOSSUtils.upload(endpoint, bucketName, file.getBytes(), extName));
        } catch (Exception e) {
            log.error("上传文件失败");
            return Result.error("上传文件失败");
        }
    }
}
